package com.base.service;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lazyzhao.util.pagination.Pagination;


public class LogsService extends BasicService{
	
	public static final String TABLENAME_LOGS = "logs";
	
	public static final String TABLENAME_LOGS_VIEW = "(select l.*,sa.* from logs l left join (select s.id service_id,s.name servicename,a.id appid,a.name appname from services s,apps a where s.app=a.id) sa on l.serviceid = sa.service_id ) Z";
	
	public Pagination<List<Map<String,Object>>> findLogs(Connection conn,Map<String,Object> params,Pagination<List<Map<String,Object>>> pagination) throws SQLException{
		return query(conn, tname(TABLENAME_LOGS), params, pagination);
	}
	
	public List<Map<String,Object>> findLogs(Connection conn,Map<String,Object> params,int... startend) throws SQLException{
		int start =0;
		int end = 999999999;
		
		if(startend.length>0){
			start = startend[0];
		}
		if(startend.length>1){
			end = startend[1];
		}
		
		return queryMapList(conn, tname(TABLENAME_LOGS), params, start, end);
	}
	
	public Pagination<List<Map<String,Object>>> findLogsView(Connection conn,Map<String,Object> params,Pagination<List<Map<String,Object>>> pagination) throws SQLException{
		return query(conn, TABLENAME_LOGS_VIEW, params, pagination);
	}
	
	public Map<String,Object> findLogsById(Connection conn,String id) throws SQLException{
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		return queryMap(conn, tname(TABLENAME_LOGS), params);
	}
	
	public int insertLogs(Connection conn,Map<String,Object> objects) throws SQLException{
		return query.insert(conn, tname(TABLENAME_LOGS), objects);
	}

	public int updateLogs(Connection conn,Map<String,Object> objects, String... wheres) throws SQLException{
		return query.update(conn, tname(TABLENAME_LOGS), objects,wheres);
	}
	public int updateLogs(Connection conn,List<Map<String,Object>> objects, String... wheres) throws SQLException{
		return query.update(conn, tname(TABLENAME_LOGS), objects,wheres);
	}

	public int deleteLogs(Connection conn,String[] ids) throws SQLException{
		Object[][] objs = new Object[ids.length][];
		for(int i=0;i<ids.length;i++){
			objs[i] = new Object[]{ids[i]}; 
		}
		int[] ints = query.batch(conn, "delete from " + tname(TABLENAME_LOGS) + " where id=?",objs);
		int count = 0;
		for(int i : ints){
			count+=i;
		}
		return count;
	}
	
	public int deleteAllLogs(Connection conn) throws SQLException{
		int count = query.update(conn, "delete from " + tname(TABLENAME_LOGS));
		return count;
	}
}
